package com.zhihaoliang.coroutine.buss.net.util.http

import android.annotation.SuppressLint
import java.security.SecureRandom
import java.security.cert.CertificateException
import java.security.cert.X509Certificate
import javax.net.ssl.SSLContext
import javax.net.ssl.SSLSocketFactory
import javax.net.ssl.X509TrustManager

/**
 * @author 支豪亮
 * @date 2022/10/14 10:14
 * @des
 */

fun createSSLSocketFactory(): SSLSocketFactory? {
    try {
        val sc = SSLContext.getInstance("TLS")
        sc.init(null, arrayOf(TrustAllCerts()),
                SecureRandom())
        return sc.socketFactory
    } catch (e: Exception) {
    }
    return null
}

private class TrustAllCerts : X509TrustManager {
    @SuppressLint("TrustAllX509TrustManager")
    @Throws(CertificateException::class)
    override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) {
    }

    @SuppressLint("TrustAllX509TrustManager")
    @Throws(CertificateException::class)
    override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) {
    }

    override fun getAcceptedIssuers(): Array<X509Certificate> {
        return arrayOf()
    }
}